package cn.yuan.xiaoyu.testmodule.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by yukuoyuan on 2017/9/5.
 */

public class PathView extends View {

    private Paint paint;
    private int count = 6;                //数据个数
    private float angle = (float) (Math.PI * 2 / count);
    private float radius;                   //网格最大半径
    private int centerX;                  //中心X
    private int centerY;                  //中心Y

    public PathView(Context context) {
        super(context);
        initpaint();
    }

    public PathView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initpaint();

    }

    public PathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initpaint();

    }

    /**
     * 初始化画笔
     */
    private void initpaint() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        radius = Math.min(h, w) / 2 * 0.9f;
        //中心坐标
        centerX = w / 2;
        centerY = h / 2;
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Path path = new Path();
        float r = radius / (count - 1);//r是蜘蛛丝之间的间距
        for (int i = 1; i < count; i++) {//中心点不用绘制
            float curR = r * i;//当前半径
            path.reset();
            for (int j = 0; j < count; j++) {
                if (j == 0) {
                    path.moveTo(centerX + curR, centerY);
                } else {
                    //根据半径，计算出蜘蛛丝上每个点的坐标
                    /**
                     * 得到每个角度的x点
                     * 算法:cos(弧度)*半径=x;
                     */
                    float x = (float) (centerX + curR * Math.cos(angle * j));
                    /**
                     * 得到每个角度的有点
                     *  算法:sin(弧度)*半径=y;
                     */
                    float y = (float) (centerY + curR * Math.sin(angle * j));
                    path.lineTo(x, y);
                }
            }
            path.close();//闭合路径
            canvas.drawPath(path, paint);
        }
        /**
         * 绘制线,到最外层的半径
         */
        for (int i = 0; i < count; i++) {
            path.reset();
            path.moveTo(centerX, centerY);

            /**
             * 得到每个角度的x点
             * 算法:cos(弧度)*半径=x;
             */
            float x = (float) (centerX + radius * Math.cos(angle * i));
            /**
             * 得到每个角度的有点
             *  算法:sin(弧度)*半径=y;
             */
            float y = (float) (centerY + radius * Math.sin(angle * i));
            path.lineTo(x, y);
            canvas.drawPath(path, paint);
        }


    }
}
